package com.zhiche.lisa.core.utils.redislog;

import com.lisa.syslog.SysLogUtil;
import com.lisa.syslog.model.SysLogDataPo;
import com.lisa.syslog.model.publics.PublicLog;
import com.zhiche.lisa.core.enums.RedisLogTypeEnum;
import com.zhiche.lisa.core.enums.SystemEnum;
import com.lisa.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;


public class RedisLogUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisLogUtil.class);

    /**
     * 添加日志记录到日志系统
     */
    public static void addSysLogs(String sourceSystem, String targetSystem, String busEventKey, String reqParam, String uri, Integer dataStatus, Integer isSuc, String errCont,Boolean isTest) {
        try{
            if (busEventKey != null && reqParam != null) {
                String key = sourceSystem + "_" + targetSystem;
                String fKey = uri+ SystemEnum.UNDERLINE.getCode()+ busEventKey + SystemEnum.UNDERLINE.getCode() + dataStatus;
                // 业务相关实体类
                PublicLog publicLog = new PublicLog(sourceSystem, targetSystem, uri, busEventKey, reqParam, dataStatus, isSuc, 1, errCont);
                SysLogDataPo sysLogDataPo = new SysLogDataPo(SysLogUtil.PUBLIC_, JSONUtil.toJsonStr(publicLog));
                SysLogUtil.addSysLogs(key, fKey, sysLogDataPo, isTest);
            }
        } catch (Exception e) {
            LOGGER.error(busEventKey+"添加日志出现异常:", e.getMessage());
        }
    }

    /**
     * 添加日志记录到日志系统
     */
    public static void addSysShipmentLogs(String sourceSystem, String targetSystem, String uri, String busEventKey, String busEventSequence, String reqParam, Integer dataStatus, Integer isSuc, String errCont, Boolean isTest) {
        try {
            if (busEventKey != null && busEventSequence != null && reqParam != null) {
                String tableIdx = RedisLogTypeEnum.SHIPMENT.getCode();
                String key = sourceSystem + "_" + targetSystem;
                String fKey = uri + SystemEnum.UNDERLINE.getCode() + busEventKey + SystemEnum.UNDERLINE.getCode() + busEventSequence + SystemEnum.UNDERLINE.getCode() + dataStatus;
                // 业务相关实体类
                LogDataPo logDataPo = new LogDataPo(sourceSystem, targetSystem, uri, busEventKey, busEventSequence, reqParam, isSuc, errCont, 1, 0, dataStatus);
                SysLogDataPo sysLogDataPo = new SysLogDataPo(tableIdx, JSONUtil.toJsonStr(logDataPo));
                SysLogUtil.addSysLogs(key, fKey, sysLogDataPo, isTest);
            }
        } catch (Exception e) {
            LOGGER.error(busEventKey+"指令下发，添加日志异常:" + e.getMessage());
        }
    }

}
